package edu.njupt.ssm.controller;

import edu.njupt.ssm.bean.Product;
import edu.njupt.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * @Title: ProductController
 * @Author:孙尊义
 * @version:1.0
 * @since:1.0
 * @Date 2024/11/5 13:13
 */
@Controller
public class ProductController {

    @Autowired
    private ProductService productService;
    //处理首页请求
    @GetMapping("/")
    public String indexHandle(Model model){
        //查询平台总交易额度
        Double turnover = productService.findTurnover();
        model.addAttribute("turnover", turnover);
        //查询所有产品
        List<Product> allProducts = productService.findAllProducts();
        model.addAttribute("allProducts", allProducts);
        return "/index.jsp";
    }
    //根据产品名称进行查询
    @GetMapping("/product/name")
    public String listHandle(Model model,String name){
        List<Product> result = productService.findProductsByName(name);
        model.addAttribute("result", result);
        model.addAttribute("name", name);
        return "/jsp/result.jsp";
    }
    //商家产品
    @PostMapping("/product/add")
    public String registerHandle(Model model,Product product){
        //添加新产品
         productService.addProduct(product);
         //查询所有产品
        List<Product> allProducts = productService.findAllProducts();
        model.addAttribute("allProducts", allProducts);
        return "/jsp/manager.jsp";
    }

    @GetMapping("/logout")
    public String logoutHandle(Model model){
        return "/index.jsp";
    }
    @GetMapping("/login")
    public String loginHandle(Model model){
        return "/index.jsp";
    }

}
